$(document).ready(function() {
	var rainPanelDate = $('.rain-panel-date');
	var rainAll = $('#rainAll');
	var rainDataTable = $('#rainDataTable');
	var rainfallStatistics = $('#rainfallStatistics');
	var layerRainTabsGroup = $('#layerRainTabsGroup');
	var outSide = $('#outSide');
	var smallReservoir = $('#smallReservoir');
	var rainStartHour = $('#rainStartHour');
	var rainEndHour = $('#rainEndHour');
	var imgSS = $('#imgSS');
	var imgZK = $('imgZK');
	var lineNumSelect = $('#lineNumSelect').val();
	$('#lineNumVal').val(lineNumSelect);
	var gisRains, dataStr, allStationStr;
	var arrayData = "";
	/*var slideMin,slideMax;*/
	var lineVal;
	var layer = $.map.layer,
		txtLayer = $.map.txtLayer,
		valLayer = $.map.valLayer,
		// 远望等值线、面
		/*kmlPointLayer=$.map.kmlPointLayer,
		kmlLineLayer= $.map.kmlLineLayer,
		kmlAreaLayer=$.map.kmlAreaLayer,*/
		//江苏服务
		dzxLayer = $.map.dzxLayer,
		dzmLayer = $.map.dzmLayer,
		createMarkerSymbol = $.map.createMarkerSymbol;


	rainStartHour.TouchSpin({
		verticalbuttons: true,
		min: 1,
		max: 23,
		initval: new Date().getHours()
	});

	rainEndHour.TouchSpin({
		verticalbuttons: true,
		min: 1,
		max: 23,
		initval: new Date().getHours()
	});
	rainfallStatistics.find('tbody').append('<tr style="height:22px"><td style="padding-left:7px;"><div class="gradeLegend1"></div></td><td>0-10</td><td id="gradeLegend1Num"></td><td><input type="checkbox" id="gradeLegend1" value="gradeLegend1" checked></td></tr>');
	rainfallStatistics.find('tbody').append('<tr style="height:22px"><td style="padding-left:7px;"><div class="gradeLegend2"></div></td><td>10-25</td><td id="gradeLegend2Num"></td><td><input type="checkbox" id="gradeLegend2" value="gradeLegend2" checked></td></tr>');
	rainfallStatistics.find('tbody').append('<tr style="height:22px"><td style="padding-left:7px;"><div class="gradeLegend3"></div></td><td>25-50</td><td id="gradeLegend3Num"></td><td><input type="checkbox" id="gradeLegend3" value="gradeLegend3" checked></td></tr>');
	rainfallStatistics.find('tbody').append('<tr style="height:22px"><td style="padding-left:7px;"><div class="gradeLegend4"></div></td><td>50-100</td><td id="gradeLegend4Num"></td><td><input type="checkbox" id="gradeLegend4" value="gradeLegend4" checked></td></tr>');
	rainfallStatistics.find('tbody').append('<tr style="height:22px"><td style="padding-left:7px;"><div class="gradeLegend5"></div></td><td>100-250</td><td id="gradeLegend5Num"></td><td><input type="checkbox" id="gradeLegend5" value="gradeLegend5" checked></td></tr>');
	rainfallStatistics.find('tbody').append('<tr style="height:22px"><td style="padding-left:7px;"><div class="gradeLegend6"></div></td><td>250以上</td><td id="gradeLegend6Num"></td><td><input type="checkbox" id="gradeLegend6" value="gradeLegend6" checked></td></tr>');

	$('input[name="dayRadio"]').on('change', function() {
		var val = $(this).val();
		queryRainData(val);
	});
	/*$('input[name="dataForm"]').on('change',function(){
		var val = $(this).val();
		if(val=='autoSet'){
			$('#lineNumSelect').attr("disabled",true);
			$('#lineNumVal').attr("disabled",true);
		}else{
			$('#lineNumSelect').attr("disabled",false);
			$('#lineNumVal').attr("disabled",false);
		}
	});*/
	$('#BasemapToggle').on('click', function() {
		var maplayerText = $("#basemapTitle").text();
		var alt1 = "卫星影像";
		var alt2 = "街道图";
		var src1 = "http://js.arcgis.com/3.7/js/esri/dijit/images/basemaps/satellite.jpg";
		var src2 = "http://js.arcgis.com/3.7/js/esri/dijit/images/basemaps/streets.jpg";
		if (maplayerText == '卫星影像') {
			baseMap.hide();
			baseMap2.show();
			$("#mapImg").attr('src', src2);
			$("#mapImg").attr('alt', alt2);
			$("#basemapTitle").text(alt2);
		} else {
			baseMap2.hide();
			baseMap.show();
			$("#mapImg").attr('src', src1);
			$("#mapImg").attr('alt', alt1);
			$("#basemapTitle").text(alt1);
		}
	});
	$('#lineNumSelect').on('change', function() {
		var val = $(this).val();
		$('#lineNumVal').val(val);
	})
	rainfallStatistics.hide();
	imgSS.hide();
	imgZK.hide();
	$('#imgSS').on('click', function() {
		$('#imgSS').hide();
		$('#rainfallStatistics').hide();
		$('#imgZK').show();
	});
	$('#imgZK').on('click', function() {
		$('#imgZK').hide();
		$('#rainfallStatistics').show();
		$('#imgSS').show();
	});

	$('#queryRainBtn').on('click', function() {
		queryRainData('freeDate', 'queryBtn');
	});

	$('#searchRainStatBtn').on('click', function() {
		var searchKey = $('#searchRainStatInput').val();
		var existsCls = rainDataTable.find('tbody tr.filterTr');
		var findTrs;
		if (existsCls.length > 0) {
			findTrs = existsCls;
		} else {
			findTrs = rainDataTable.find('tbody tr').not(':hidden');
			findTrs.addClass('filterTr');
		}
		var hideTr = findTrs.filter(':not(:contains("' + searchKey + '"))');
		var showTr = findTrs.filter(':contains("' + searchKey + '")');
		if (searchKey != "") {
			$.each(showTr, function(index, item) {
				$(item).find('td:eq(0)').text(parseInt(index) + 1);
			});
		}
		showTr.show();
		hideTr.hide();
	});
	$('#clearBtn').on('click', function() {
		// 远望等值线、面
		/*kmlPointLayer.clear();
		kmlLineLayer.clear();
		kmlAreaLayer.clear();*/
		//江苏
		dzxLayer.clear();
		var i = map.getLevel();
		if (map.getLevel() >= 8) {
			txtLayer.show();
		} else {
			valLayer.show();
		}
		isKmlClear = true;
		layer.show();
	});
	$("#showBtn").on('click', function() {
		if (map.getLevel() >= 8) {
			txtLayer.show();
		} else if (map.getLevel() == 7) {
			valLayer.show();
		}
		layer.show();
		isKmlClear = true;
	});
	$("#hideBtn").on('click', function() {
		layer.hide();
		txtLayer.hide();
		valLayer.hide();
		isKmlClear = false;
	});
	$('#drawVisionBtn').on('click', function() {
		$.buildDialog({
			$_this: null,
			target: $('#drawVision'),
			title: '绘制等值线',
			/*modal: true,*/
			width: 300,
			height: 180,
			close: function() {
				$(this).dialog('destroy');
			}
		});
	});

	//获取100、150、188、280站点数据（远望服务）
	/*function handleStationStr(lineNum,drawType,dataType){
		arrayData="[";
		$.post('../gis/StationScreen.json?method=getStatStcd',{statKey:dataType,r:new Date().getTime()},function(data){
			for(var i=0;i<dataStr.length;i++){
				if(data.indexOf(dataStr[i].stcd)>0){
					arrayData+="[\""+dataStr[i].lgtd+"\",";
					arrayData+="\""+dataStr[i].lttd+"\",";
					arrayData+="\""+dataStr[i].val+"\",";
					arrayData+="\""+dataStr[i].stcd+dataStr[i].val+"\"";
					arrayData+="],"
				}
			}
			arrayData.substring(0, arrayData.length-1);
			arrayData+="]";
			makeKml(lineNum,drawType,arrayData,"drawBtnType");
		});
	}*/
	//获取100、150、188、280站点数据（江苏服务）
	function handleStationStr(lineNum, drawType, dataType) {
		$.post('../gis/StationScreen.json?method=getStatStcd', {
			statKey: dataType,
			r: new Date().getTime()
		}, function(data) {
			for (var i = 0; i < dataStr.length; i++) {
				if (data.indexOf(dataStr[i].stcd) > 0) {
					arrayData += dataStr[i].lgtd + ",";
					arrayData += dataStr[i].lttd + ",";
					arrayData += dataStr[i].val + ",";
					arrayData += dataStr[i].stnm + ",";
					arrayData += dataStr[i].stcd + "|"
				}
			}
			var rainData = arrayData.substring(0, arrayData.length - 1);
			makeKml(lineNum, drawType, rainData);
		});
	}
	//江苏
	$('input[name="drawType"]').on('change', function() {
		var drawType = $('input[name="drawType"]:checked').val();
		if (drawType == 'visoline') {
			dzmLayer.hide();
			dzxLayer.show();
		} else {
			dzxLayer.hide();
			dzmLayer.show();
		}
	});

	/*$('input[name="drawType"]').on('change',function(){
		var drawType=$('input[name="drawType"]:checked').val();
	});*/
	$('#visionBtn').on('click', function() {
		//计算方法（自动计算/人工定义）
		/*var dataForm =$('input[name="dataForm"]:checked').val();*/
		//类型（等值线、等值面、彩虹图）
		var drawType = $('input[name="drawType"]:checked').val();

		var dataType = $('#dataType').val();
		var lineVal = $('#lineNumVal').val();
		/*$('#visionBtn').attr("disabled", true);*/
		var code = getCookie("code");

		if (dataType == 'allStat') {
			arrayData = allStationStr;
			//远望绘制等值线、面方法调用
			/*makeKml(lineVal,drawType,arrayData,"drawBtnType");*/
			//江苏绘制方法调用
			makeKml(lineVal, drawType, arrayData);
		} else {
			handleStationStr(lineVal, drawType, dataType);
		}

	});

	$("#stcdShow").on('change', function() {
		if (this.checked == true) {
			if (map.getLevel() >= 7) {
				txtLayer.show();
				valLayer.hide();
			} else if (5 >= map.getLevel()) {
				txtLayer.hide();
				valLayer.hide();
			} else {
				txtLayer.hide();
				valLayer.show();
			}
			layer.show();
		} else {
			layer.hide();
			txtLayer.hide();
			valLayer.hide();
		}
	});
	$("#visoLineShow").on('change', function() {
		var drawType = $('input[name="drawType"]:checked').val();
		if (this.checked == true && drawType == 'visoline') {
			dzxLayer.show();
			dzmLayer.hide();
		} else if (this.checked == true && drawType == 'visoface') {
			dzxLayer.hide();
			dzmLayer.show();
		} else {
			dzxLayer.hide();
			dzmLayer.hide();
		}
	});
	//远望绘制等值线、面调用
	/*function readKml(){
		$.post('../gis/VisoLineScreen.json?method=readKml',function(data){
			if(data.kmlAreas!=null){
				for(var i=0;i<data.kmlAreas.length;i++){
					var pointStr=data.kmlAreas[i].points;
					var areaColor="#"+data.kmlAreas[i].areaColor;
					var outLineColor="#"+data.kmlAreas[i].outLineColor;
					pointStr=pointStr.substring(1,pointStr.length-1);
					
					var polygon = new esri.geometry.Polygon();
					var kmlAreasymbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, 
							new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID,
									new dojo.Color(areaColor), 1),new dojo.Color(areaColor));
					var iarry=new Array();
					var kmlAreaArray = pointStr.split(",");
					arrayLen = kmlAreaArray.length;
					for(var j=0;j<(arrayLen/2)-1;j++){
						iarry[j]=new Array(); 
						iarry[j][0]=kmlAreaArray[j*2];
						iarry[j][1]=kmlAreaArray[j*2+1];
					}
					polygon.addRing(iarry);
					var polygonGraphic = new esri.Graphic(polygon, kmlAreasymbol);
					kmlAreaLayer.add(polygonGraphic);
					
				}
			}
			if(data.kmlPoints!=null){
				for(var i=0;i<data.kmlPoints.length;i++){
					var pointVal=data.kmlPoints[i].pointVal;
					var pointStr=data.kmlPoints[i].points;
					pointStr=pointStr.substring(1,pointStr.length-1);
					var lgtdAndlttd=pointStr.split(",");
					var kmlPoint =  new esri.geometry.Point(parseFloat(lgtdAndlttd[0]),parseFloat(lgtdAndlttd[1]));
					var kmlPointTextSymbol = new esri.symbol.TextSymbol(pointVal);
					kmlPointTextSymbol.setColor("#FF0000");
					kmlPointTextSymbol.setFont(font);
					var kmlPointValGraphic = new esri.Graphic(kmlPoint,kmlPointTextSymbol);
					kmlPointLayer.add(kmlPointValGraphic);
				}
			}
			
			if(data.kmlLines!=null){
				for(var i=0;i<data.kmlLines.length;i++){
					var graphics = [];
					var pointStr=data.kmlLines[i].points;
					var color="#"+data.kmlLines[i].color;
					var width=data.kmlLines[i].width;
					
					var width=data.kmlLines[i].width;
					pointStr=pointStr.substring(1,pointStr.length-1);
					var points = pointStr.split(';');
					var dataIntArr = [];
					$.each(points,function(i,n){
						dataIntArr.push(n.split(','));
					});
					var kmlLinesymbol = new esri.symbol.SimpleFillSymbol(
							esri.symbol.SimpleFillSymbol.STYLE_NULL,
						new esri.symbol.SimpleLineSymbol(
							esri.symbol.SimpleLineSymbol.STYLE_SOLID,
							new dojo.Color("#FF0000"),1
					));
					var polygon = new esri.geometry.Polygon();
					polygon.addRing(dataIntArr);
					var graphic = new esri.Graphic(polygon,kmlLinesymbol);
					kmlLineLayer.add(graphic);
					graphics.push(graphic);
				}
			}
			kmlPointLayer.show();
			kmlLineLayer.show();
			kmlAreaLayer.show();
		});
	}*/
	/*function makeKml(lineNum,drawType,arrayData,drawBtnType){
		$.post('../gis/VisoLineScreen.json?method=getKml',{lineNum:lineNum,drawType:drawType,arrayData:arrayData,r:new Date().getTime()},function(data){
			kmlPointLayer.hide();
			kmlLineLayer.clear();
			kmlAreaLayer.clear();
			kmlLineLayer.show();
			kmlAreaLayer.show();
			if(data.kmlAreas!=null){
				for(var i=0;i<data.kmlAreas.length;i++){
					var pointStr=data.kmlAreas[i].points;
					var areaColor="#"+data.kmlAreas[i].areaColor;
					var outLineColor="#"+data.kmlAreas[i].outLineColor
					pointStr=pointStr.substring(1,pointStr.length-1);
					
					var polygon = new esri.geometry.Polygon();
					var kmlAreasymbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, 
							new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID,
									new dojo.Color(areaColor), 1),new dojo.Color(areaColor));
					var iarry=new Array();
					var kmlAreaArray = pointStr.split(",");
					arrayLen = kmlAreaArray.length;
					for(var j=0;j<(arrayLen/2)-1;j++){
						iarry[j]=new Array(); 
						iarry[j][0]=kmlAreaArray[j*2];
						iarry[j][1]=kmlAreaArray[j*2+1];
					}
					polygon.addRing(iarry);
					var polygonGraphic = new esri.Graphic(polygon, kmlAreasymbol);
					kmlAreaLayer.add(polygonGraphic);
					
				}
			}
			if(data.kmlPoints!=null){
				for(var i=0;i<data.kmlPoints.length;i++){
					var pointVal=data.kmlPoints[i].pointVal;
					var pointStr=data.kmlPoints[i].points;
					pointStr=pointStr.substring(1,pointStr.length-1);
					var lgtdAndlttd=pointStr.split(",");
					var kmlPoint =  new esri.geometry.Point(parseFloat(lgtdAndlttd[0]),parseFloat(lgtdAndlttd[1]));
					var kmlPointTextSymbol = new esri.symbol.TextSymbol(pointVal);
					kmlPointTextSymbol.setColor("#FF0000");
					kmlPointTextSymbol.setFont(font);
					var kmlPointValGraphic = new esri.Graphic(kmlPoint,kmlPointTextSymbol);
					kmlPointLayer.add(kmlPointValGraphic);
				}
			}
			
			if(data.kmlLines!=null){
				for(var i=0;i<data.kmlLines.length;i++){
					var graphics = [];
					var pointStr=data.kmlLines[i].points;
					var color="#"+data.kmlLines[i].color;
					var width=data.kmlLines[i].width;
					
					var width=data.kmlLines[i].width;
					pointStr=pointStr.substring(1,pointStr.length-1);
					var points = pointStr.split(';');
					var dataIntArr = [];
					$.each(points,function(i,n){
						dataIntArr.push(n.split(','));
					});
					var kmlLinesymbol = new esri.symbol.SimpleFillSymbol(
							esri.symbol.SimpleFillSymbol.STYLE_NULL,
						new esri.symbol.SimpleLineSymbol(
							esri.symbol.SimpleLineSymbol.STYLE_SOLID,
							new dojo.Color("#FF0000"),width
					));
					var polygon = new esri.geometry.Polygon();
					polygon.addRing(dataIntArr);
					var graphic = new esri.Graphic(polygon,kmlLinesymbol);
					kmlLineLayer.add(graphic);
					graphics.push(graphic);
				}
			}
			if($("#stcdShow").is(':checked')==false){
				layer.hide();
				txtLayer.hide();
				valLayer.hide();
			}else{
				if(map.getLevel()>= 8){
					txtLayer.show();
				}else if(map.getLevel()==7){
					valLayer.show();
				}
				layer.show();
			}
			
			$('#visionBtn').removeAttr("disabled");
			layer.hide();
			txtLayer.hide();
			valLayer.hide();
			isKmlClear = false;
		});
	}*/
	//江苏等值线绘制方法
	function makeKml(lineNum, drawType, arrayData) {
		$.post('../gis/AjaxMakeIso.json?method=getIso', {
			sdate: "201512178",
			edate: "201512188",
			rainLevel: lineNum,
			drawType: drawType,
			rainData: arrayData,
			r: new Date().getTime()
		}, function(data) {
			dzxLayer.clear();
			dzmLayer.clear();
			$("#visoLineShow").prop("checked", true);
			if (drawType == 'visoline') {
				dzmLayer.hide();
				dzxLayer.show();
			} else {
				dzxLayer.hide();
				dzmLayer.show();
			}
			for (var n = 0; n < data.length; n++) {
				var IsoData = data[n];
				if (IsoData.isoSurfaces != null) {
					for (var i = 0; i < IsoData.isoSurfaces.length; i++) {
						var pointStr = IsoData.isoSurfaces[i].points;
						var areaColor = "#" + IsoData.isoSurfaces[i].areaColor;

						var polygon = new esri.geometry.Polygon();
						var isoSurfacesymbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID,
							new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID,
								new dojo.Color(areaColor), 1), new dojo.Color(areaColor));
						var iarry = new Array();
						var isoSurfacesArray = pointStr.split(",");
						arrayLen = isoSurfacesArray.length;
						for (var j = 0; j < (arrayLen / 2) - 1; j++) {
							iarry[j] = new Array();
							iarry[j][0] = isoSurfacesArray[j * 2];
							iarry[j][1] = isoSurfacesArray[j * 2 + 1];
						}
						polygon.addRing(iarry);
						var polygonGraphic = new esri.Graphic(polygon, isoSurfacesymbol);
						if (n == 1) {
							dzxLayer.add(polygonGraphic);
						} else {
							dzmLayer.add(polygonGraphic);
						}
					}
				}
				if (IsoData.isoPoints != null) {
					for (var i = 0; i < IsoData.isoPoints.length; i++) {
						var pointVal = IsoData.isoPoints[i].pointVal;
						var pointStr = IsoData.isoPoints[i].points;
						var lgtdAndlttd = pointStr.split(",");
						var isoPoint = new esri.geometry.Point(parseFloat(lgtdAndlttd[0]), parseFloat(lgtdAndlttd[1]), {
							"wkid": 4490
						});
						var isoPointTextSymbol = new esri.symbol.TextSymbol(pointVal);
						isoPointTextSymbol.setColor("#FF0000");
						var isoPointValGraphic = new esri.Graphic(isoPoint, isoPointTextSymbol);
						if (n == 1) {
							dzxLayer.add(isoPointValGraphic);
						} else {
							dzmLayer.add(isoPointValGraphic);
						}

					}
				}

				if (IsoData.isoLines != null) {
					for (var i = 0; i < IsoData.isoLines.length; i++) {
						var graphics = [];
						var pointStr = IsoData.isoLines[i].points;
						var color = "#" + IsoData.isoLines[i].color;
						var points = pointStr.split(';');
						var dataIntArr = [];
						$.each(points, function(i, n) {
							dataIntArr.push(n.split(','));
						});
						var isoLinesymbol = new esri.symbol.SimpleFillSymbol(
							esri.symbol.SimpleFillSymbol.STYLE_NULL,
							new esri.symbol.SimpleLineSymbol(
								esri.symbol.SimpleLineSymbol.STYLE_SOLID,
								new dojo.Color("#FF0000"), 2
							));
						var polygon = new esri.geometry.Polygon();
						polygon.addRing(dataIntArr);
						var graphic = new esri.Graphic(polygon, isoLinesymbol);
						if (n == 1) {
							dzxLayer.add(graphic);
						} else {
							dzmLayer.add(graphic);
						}
					}
				}
			}

			if ($("#stcdShow").is(':checked') == false) {
				layer.hide();
				txtLayer.hide();
				valLayer.hide();
			} else {
				if (map.getLevel() >= 7) {
					txtLayer.show();
					valLayer.hide();
				} else if (5 >= map.getLevel()) {
					txtLayer.hide();
					valLayer.hide();
				} else {
					txtLayer.hide();
					valLayer.show();
				}
				layer.show();
			}
			isKmlClear = false;
		});
	}

	function getRainChooseTabsAndIsProv() {
		var chooseTabs = [];
		rainfallStatistics.find(':checked').each(function(index, item) {
			chooseTabs.push($(item).val());
		});
		var isProv = $('#outSide').prop('checked');
		var isSmallReservoir = $('#smallReservoir').prop('checked');
		return [chooseTabs, isProv, isSmallReservoir];
	}

	rainfallStatistics.find(':checkbox').on('change', function() {
		var chooseTabsAndIsProv = getRainChooseTabsAndIsProv();
		var chooseTabs = chooseTabsAndIsProv[0];

		var checks = rainfallStatistics.find(':checkbox').not('#outSide');
		if (this.id == 'rainAll' && this.checked == false) {
			$.each(checks, function(i, n) {
				$(this).prop('checked', false);
				chooseTabs.splice($.inArray(this.value, chooseTabs), 1);
			});
		} else if (this.id == 'rainAll' && this.checked == true) {
			$.each(checks, function(i, n) {
				$(this).prop('checked', true);
				if (this.id != 'rainAll') {
					chooseTabs.push(this.id);
				}
			});
		}
		var isProv = chooseTabsAndIsProv[1];
		var isSmall = chooseTabsAndIsProv[2];
		var txtGraphics = txtLayer.graphics;
		var valGraphics = valLayer.graphics;

		tableBodyTrs = rainDataTable.find('tbody tr');
		tableBodyFirstTds = rainDataTable.find('.firstTd');
		var addIndex = 0;
		$.each(layer.graphics, function(index, item) {
			var attr = item.attributes;
			var gradeLegend = attr.gradeLegend;
			var inProvOrInCity = attr.inProvOrInCity;
			var littleReservoir = attr.smallReservoir;
			if ($.inArray(gradeLegend, chooseTabs) >= 0) {
				if ((isProv && inProvOrInCity == 0) || inProvOrInCity == 1) {
					if ((isSmall && littleReservoir == false) || littleReservoir) {
						item.show();
						txtGraphics[index].show();
						valGraphics[index].show();
						var td = tableBodyFirstTds.eq(index);
						td.text(++addIndex);
						td.parent().show();
					} else {
						item.hide();
						txtGraphics[index].hide();
						valGraphics[index].hide();
						txtLayer.graphics[index].hide();
						valLayer.graphics[index].hide();
						tableBodyTrs.eq(index).hide();
					}
				} else {
					item.hide();
					txtGraphics[index].hide();
					valGraphics[index].hide();
					txtLayer.graphics[index].hide();
					valLayer.graphics[index].hide();
					tableBodyTrs.eq(index).hide();
				}
			} else {
				item.hide();
				txtGraphics[index].hide();
				valGraphics[index].hide();
				tableBodyTrs.eq(index).hide();
			}
		});
	});

	outSide.on('change', function() {
		var isChoose = $(this).prop('checked');
		var isSmallChecked = $('#smallReservoir').prop('checked');
		var addIndex = 0;
		var tableBodyTrs = rainDataTable.find('tbody tr');
		var tableBodyFirstTds = rainDataTable.find('.firstTd');
		rainDataTable.find('.filterTr').removeClass('filterTr');
		$('#searchRainStatInput').val('');
		var chooseTabsAndIsProv = getRainChooseTabsAndIsProv();
		var chooseTabs = chooseTabsAndIsProv[0];
		$.each(layer.graphics, function(index, item) {
			var visible = item.visible;
			var inProvOrInCity = item.attributes.inProvOrInCity;
			var small = item.attributes.smallReservoir;
			var val = item.attributes.val;
			var gradeLegend = item.attributes.gradeLegend;

			if ((isChoose || (visible && inProvOrInCity == 1)) && (isSmallChecked || small == false)) { // 选中省外
				if ($.inArray(gradeLegend, chooseTabs) >= 0) {
					item.show();
					txtLayer.graphics[index].show();
					valLayer.graphics[index].show();
					var td = tableBodyFirstTds.eq(index);
					td.text(++addIndex);
					td.parent().show();
				}
			} else if (((!isChoose && inProvOrInCity == 0) || !visible) || (!isSmallChecked && small)) { // 未选中省外
				item.hide();
				valLayer.graphics[index].hide();
				txtLayer.graphics[index].hide();
				tableBodyTrs.eq(index).hide();
			}
		});
	});

	smallReservoir.on('change', function() {
		var isChoose = $(this).prop('checked'); //小水库勾选
		var isOutSideChecked = outSide.prop('checked');
		var addIndex = 0;
		var tableBodyTrs = rainDataTable.find('tbody tr');
		var tableBodyFirstTds = rainDataTable.find('.firstTd');
		rainDataTable.find('.filterTr').removeClass('filterTr');
		$('#searchRainStatInput').val('');
		var chooseTabsAndIsProv = getRainChooseTabsAndIsProv();
		var chooseTabs = chooseTabsAndIsProv[0];
		$.each(layer.graphics, function(index, item) {
			var small = item.attributes.smallReservoir;
			var gradeLegend = item.attributes.gradeLegend;
			var visible = item.visible;
			var inProvOrInCity = item.attributes.inProvOrInCity;

			if ((isOutSideChecked || inProvOrInCity == 1) && (isChoose || small == false)) { // 勾选了小水库
				if ($.inArray(gradeLegend, chooseTabs) >= 0) {
					item.show();
					txtLayer.graphics[index].show();
					valLayer.graphics[index].show();
					var td = tableBodyFirstTds.eq(index);
					td.text(++addIndex);
					td.parent().show();
				}
			} else if (((!isOutSideChecked && inProvOrInCity == 0)) || (!isChoose && small)) { // 未勾选小水库
				item.hide();
				valLayer.graphics[index].hide();
				txtLayer.graphics[index].hide();
				tableBodyTrs.eq(index).hide();
			}
		});
	});

	var graphicHighlight;
	var highlightPointVal;

	rainDataTable.find('tbody').on('click', 'tr', function() {
		var $_this = $(this);
		rainDataTable.find('.tableTrColor').removeClass('tableTrColor');
		$_this.addClass('tableTrColor');

		if ($.isEmptyObject($_this.data())) {
			return;
		}

		var lgtd = $_this.data('lgtd');
		var lttd = $_this.data('lttd');
		var stcd = $_this.data('stcd');
		var stnm = $_this.data('stnm');
		highlightPointVal = $_this.data('val');
		var point = new esri.geometry.Point(lgtd, lttd, map.spatialReference);
		var attr = {
			stcd: stcd,
			stnm: stnm,
			val: highlightPointVal,
			callType: 'rain'
		};
		var graphic = new esri.Graphic(point, null);
		map.centerAndZoom(graphic.geometry, 10);

		if (graphicHighlight) {
			map.graphics.remove(graphicHighlight);
		}

		var highlight = new esri.symbol.SimpleMarkerSymbol(
			esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE, 22,
			new esri.symbol.SimpleLineSymbol(
				esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color('#ff0000'), 3.5),
			new dojo.Color([0, 0, 255, 0.01])
		);
		graphicHighlight = new esri.Graphic(graphic.geometry, highlight, attr);
		map.graphics.add(graphicHighlight);

	});

	layerRainTabsGroup.on('click', 'table tbody tr', function() {
		var $_this = $(this);
		var astnm = $_this.data('astnm');
		var astcd = $_this.data('astcd');
		if (astnm != '' && astcd != '') {
			$.map.makeWindow(astnm, astcd, 'layerRain');
		}
	});

	function queryRainData(val, type) {
		var now = new Date();
		var date = now.getDate();
		var startDay, endDay, startHour, endHour, startTime, endTime;

		if (!type) {
			if (val == 'freeDate') {
				rainPanelDate.show();

				endTime = $.format.date(now.setDate(date), 'yyyy-MM-dd');
				startTime = $.format.date(now.setDate(date - 3), 'yyyy-MM-dd');
				rainPanelDate.find('input[name="startTime"]').val(startTime);
				rainPanelDate.find('input[name="endTime"]').val(endTime);
			} else {
				rainPanelDate.hide();

				if (val == 'nowDay') {
					startTime = endTime = $.format.date(now, 'yyyy-MM-dd');
				} else if (val == 'yestDay') {
					startTime = endTime = $.format.date(now.setDate(date - 1), 'yyyy-MM-dd');
				} else if (val == 'lastThreeDay') {
					endTime = $.format.date(now, 'yyyy-MM-dd');
					startTime = $.format.date(now.setDate(date - 3), 'yyyy-MM-dd');
				}

				drawMapPoint(startTime, endTime, val);
			}
		} else {
			startHour = rainStartHour.val();
			endHour = rainEndHour.val();
			startDay = rainPanelDate.find('input[name="startTime"]').val();
			endDay = rainPanelDate.find('input[name="endTime"]').val();
			startTime = new Date(startDay).setHours(startHour, 0, 0, 0);
			endTime = new Date(endDay).setHours(endHour, 0, 0, 0);
			/*startTime=$.format.date(startTime,"yyyy-MM-dd HH:mm");
			endTime=$.format.date(endTime,"yyyy-MM-dd HH:mm");*/
			drawMapPoint(startTime, endTime, val);
		}
	}

	function drawMapPoint(startTime, endTime, val) {
		layer.clear();
		txtLayer.clear();
		valLayer.clear();
		//远望清除等值线、面层
		/*kmlPointLayer.clear();
		kmlLineLayer.clear();
		kmlAreaLayer.clear();*/
		//江苏等值线、面清除
		dzxLayer.clear();
		dzmLayer.clear();

		isKmlClear = true;
		/*$("#visoLineShow").prop('checked',false);*/
		$("#queryRainBtn").attr("disabled", true);
		$('input[name="dayRadio"]').attr("disabled", true);
		rainDataTable.find('tbody *').remove();
		layerRainTabsGroup.find('*').remove();
		//rainfallStatistics.find('tbody *').remove();
		var jiangyu = xiaoyus = zhongyus = dayus = baoyus = dabaoyus = tedabaoyus = {
			inSlide: [],
			outSlide: [],
			isSmallReservoir: [],
			notSmallReservoir: []
		};
		var day = Math.floor((endTime - startTime) / (24 * 3600 * 1000));
		if (day > 1) {
			rainfallStatistics.hide();
			imgSS.hide();
			imgZK.hide();
		} else {
			rainfallStatistics.show();
			imgSS.show();
			imgZK.show();
		}
		startTime = $.format.date(startTime, "yyyy-MM-dd HH:mm");
		endTime = $.format.date(endTime, "yyyy-MM-dd HH:mm");
		layerRainTabsGroup.hide();
		layerRainTabsGroup.append('<ul class="nav nav-tabs nav-tabs-xs" role="tablist" id="layerRainNavs"></ul>');
		layerRainTabsGroup.append('<div class="tab-content"></div>');
		$.get('../gis/RainfallScreen.json?method=getGisRainLayerRainfall', {
			bTm: startTime,
			eTm: endTime,
			type: val,
			r: new Date().getTime()
		}, function(data) {
			/*lineVal=data.lineNumVal;*/
			/*dataForm=data.dataForm;*/
			gisRains = data.gisRains;
			dataStr = data.dataStr;
			allStationStr = data.allStationStr;
			/*$("#lineNumVal").val(lineVal);*/
			/*if(dataForm=="selfSet"){
				$("#selfSet").attr("checked",true);
				$("#lineNumVal").removeAttr("disabled");
				$("#lineNumSelect").removeAttr("disabled");
			}else{
				$("#selfSet").removeAttr("checked");
				$("#autoSet").attr("checked",true);
			}*/
			$("#stcdShow").prop("checked", true);
			if (data.gisRains.length > 0) {
				var rainDataTBody = rainDataTable.find('tbody');
				var addIndex = 0;
				var sizeItem = gisRains.length;
				var isChoose = outSide.prop('checked');
				var isSmallReservoirChoose = smallReservoir.prop('checked');
				var csvDatas = '序号,站名,数值';
				$.each(gisRains, function(index, item) {
					if (item.val != 0.0) {
						var gradeLegend = item.gradeLegend;
						var inProvOrInCity = item.inProvOrInCity;
						var lgtd = parseFloat(item.lgtd);
						var lttd = parseFloat(item.lttd);
						var isSmallReservoir = item.smallReservoir;
						var temps;
						switch (gradeLegend) {
							case 'gradeLegend1':
								temps = xiaoyus;
								break;
							case 'gradeLegend2':
								temps = zhongyus;
								break;
							case 'gradeLegend3':
								temps = dayus;
								break;
							case 'gradeLegend4':
								temps = baoyus;
								break;
							case 'gradeLegend5':
								temps = dabaoyus;
								break;
							case 'gradeLegend6':
								temps = tedabaoyus;
								break;
						}
						if (inProvOrInCity == 0) {
							temps.outSlide.push(item);
						} else if (inProvOrInCity == 1) {
							temps.inSlide.push(item);
						}
						if (smallReservoir) {
							temps.isSmallReservoir.push(item);
						} else {
							temps.notSmallReservoir.push(item);
						}
						var attr = {
							stcd: item.stcd,
							stnm: item.stnm,
							val: item.val,
							inProvOrInCity: item.inProvOrInCity,
							smallReservoir: item.smallReservoir,
							lgtd: lgtd,
							lttd: lttd,
							gradeLegend: item.gradeLegend,
							callType: 'rain'
						};

						var csvData = (index + 1) + ',' + attr.stnm + ',' + attr.val;
						csvDatas += ('||' + csvData);
						var valTextSymbol = new esri.symbol.TextSymbol(item.val).setColor(new esri.Color([119, 136, 153]));
						valTextSymbol.yoffset = 8;
						var textSymbol = new esri.symbol.TextSymbol(item.stnm + item.val).setColor(new esri.Color([119, 136, 153]));
						textSymbol.yoffset = 8;

						var point = new esri.geometry.Point(lgtd, lttd);
						var pointColor = "";
						if (attr.gradeLegend == "gradeLegend1") {
							pointColor = "#91eb6d";
						} else if (attr.gradeLegend == "gradeLegend2") {
							pointColor = "#388a18";
						} else if (attr.gradeLegend == "gradeLegend3") {
							pointColor = "#1acec8";
						} else if (attr.gradeLegend == "gradeLegend4") {
							pointColor = "#176ae6";
						} else if (attr.gradeLegend == "gradeLegend5") {
							pointColor = "#eb13d1";
						} else if (attr.gradeLegend == "gradeLegend6") {
							pointColor = "#f71046";
						}
						var pointSymbol = new esri.symbol.SimpleMarkerSymbol().setStyle(esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE).setColor(new dojo.Color(pointColor));
						pointSymbol.setSize("6");
						var pointGraphic = new esri.Graphic(point, pointSymbol, attr);
						var pointTxtGraphic = new esri.Graphic(point, textSymbol, pointSymbol);
						var pointValGraphic = new esri.Graphic(point, valTextSymbol, pointSymbol);

						var tr = $('<tr><td class="firstTd"></td><td>' + attr.stnm + '</td><td>' + attr.val + '</td></tr>');
						tr.data('lgtd', attr.lgtd).data('lttd', attr.lttd).data('val', attr.val).data('stnm', attr.stnm).data('stcd', attr.stcd);
						if ((!isChoose && item.inProvOrInCity == 0) || (!isSmallReservoirChoose && item.smallReservoir)) {
							pointGraphic.hide();
							pointTxtGraphic.hide();
							pointValGraphic.hide();
							tr.hide();
						} else {
							pointGraphic.show();
							pointTxtGraphic.show();
							pointValGraphic.show();
							tr.find('td:eq(0)').text(++addIndex);
						}
						rainDataTBody.append(tr);

						layer.add(pointGraphic);
						txtLayer.add(pointTxtGraphic);
						valLayer.add(pointValGraphic);
					}

				});
				slideMin = Math.floor(data.slideMin), slideMax = Math.ceil(data.slideMax);
				rainDataTable.data('csvDatas', csvDatas);

				var renderer = new esri.renderer.SimpleRenderer(
					createMarkerSymbol());
				layer.setRenderer(renderer);
				layer.redraw();
				txtLayer.hide();
				valLayer.hide();

				/*buildSlider(slideMin,slideMax);*/
			} else { // 结果集为空时，填充空表格
				for (var i = 0; i < 5; i++) {
					rainDataTable.find('tbody').append('<tr style="height:22px"><td></td><td></td><td></td></tr>');
				}
				/*buildSlider(0,100);*/
			}
			$('#gradeLegend1Num').text(data.gradeLegend1);
			$('#gradeLegend2Num').text(data.gradeLegend2);
			$('#gradeLegend3Num').text(data.gradeLegend3);
			$('#gradeLegend4Num').text(data.gradeLegend4);
			$('#gradeLegend5Num').text(data.gradeLegend5);
			$('#gradeLegend6Num').text(data.gradeLegend6);
			rainfallStatistics.show();
			if (data.layerRainfalls.length > 0) {
				var navTabs = layerRainTabsGroup.find('.nav-tabs');
				var tabContents = layerRainTabsGroup.find('.tab-content');

				navTabs.find('*').remove();
				tabContents.find('*').remove();

				$.each(data.layerRainfalls, function(index, item) {
					var acode = item.acode,
						atype = item.atype,
						simpleStatGList = item.simpleStatGList;

					var id = 'tab_' + acode;
					var navTab = $('<li role="presentation" class="' + (index == 0 ? 'active' : '') + '"><a href="#' + id + '" aria-controls="' + id + '" role="tab" data-toggle="tab">' + atype + '</a></li>');
					var tabContent = $('<div role="tabpanel" class="tab-pane ' + (index == 0 ? 'active' : '') + '" style="overflow: auto;max-height:90px" id="' + id + '"></div>');

					var table = $('<table class="table table-bordered table-hover table-striped"></table>');
					table.append('<thead><tr><th>名称</th><th>降雨量</th></tr></thead>');
					var tbody = $('<tbody></tbody>');

					if (simpleStatGList.length > 0) {
						$.each(simpleStatGList, function(sindex, sitem) {
							var tr = $('<tr><td>' + sitem.astnm + '</td><td>' + sitem.value + '</td></tr>');
							tr.data('astcd', sitem.astcd).data('astnm', sitem.astnm);
							tbody.append(tr);
						});
					} else {
						for (var i = 0; i < 5; i++) {
							var tr = $('<tr style="height:22px"><td></td><td></td></tr>');
							tr.data('astcd', "").data('astnm', "");
							tbody.append(tr);
						}
					}
					table.append(tbody);
					tabContent.append(table);

					navTabs.append(navTab);
					tabContents.append(tabContent);
				});

				layerRainTabsGroup.show();
			} else {
				layerRainTabsGroup.hide();
			}
			$('#layerRainNavs').tabdrop();
			$('input[name="dayRadio"]').removeAttr("disabled");
			$("#queryRainBtn").removeAttr("disabled");
			$.map.reExtent();
		});
	}

	/*function buildSlider(min,max){
		var rainSliderArea = $('#rainSliderArea');
		rainSliderArea.find('*').remove();
		rainSliderArea.append("<input id='rainSlider' type='text' style='display:none;'/>");
		var $_rainSlider = $("#rainSlider",rainSliderArea);
		var rainSlider = $_rainSlider.data("ionRangeSlider");
		
		$_rainSlider.ionRangeSlider({
			type:'double',
			min:min,
			max:max,
			grid: true,
		    grid_num: 10,
		    onFinish:function(data){
		    	slideMin = data.from;
		    	slideMax = data.to;
		    	var layerGraphics = layer.graphics;
				var txtGraphics = txtLayer.graphics;
				var tableBodyTrs = rainDataTable.find('tbody tr');
				var isChoose = outSide.prop('checked');
				var chooseTabsAndIsProv = getRainChooseTabsAndIsProv();
				var chooseTabs = chooseTabsAndIsProv[0];
				tableBodyTrs.show();
				var tabIndex = 0;
				rainDataTable.find('.filterTr').removeClass('filterTr');
				$('#searchRainStatInput').val('');
				$.each(layerGraphics,function(index,item){
					var attr = item.attributes;
					var visible = item.visible;
					
					if(slideMin<=attr.val&&slideMax>=attr.val &&chooseTabs.indexOf(attr.gradeLegend)>-1){
						if(!isChoose && attr.inProvOrInCity == 0){
							tableBodyTrs.eq(index).hide();
						}else{
							item.show();
							txtGraphics[index].show();
							tableBodyTrs.eq(index).find('td:eq(0)').text(++tabIndex);
						}
					}else{
						item.hide();
						txtGraphics[index].hide();
						tableBodyTrs.eq(index).hide();
					}
					
					if(highlightPointVal<slideMin||slideMax<highlightPointVal){
						map.graphics.remove(graphicHighlight);
					}
				});
		    }
		});
	}*/
	$('input[name="dayRadio"]:checked').trigger('change');
	/*$('input[name="dataForm"]:checked').trigger('change');*/
});